package kr.co.bnsmedia.touchconfig;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;

public class TestEventActivity extends Activity {

	private MyView mMyView = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//TODO setContentView(R.layout.activity_testevent);
		mMyView = new MyView(this);
		setContentView(mMyView);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_LEFT:
			MyApp.log("onKeyDown : DPAD_LEFT");
			return true;
		case KeyEvent.KEYCODE_DPAD_UP:
			MyApp.log("onKeyDown : DPAD_UP");
			return true;
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			MyApp.log("onKeyDown : DPAD_RIGHT");
			return true;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			MyApp.log("onKeyDown : DPAD_DOWN");
			return true;
		default:
			MyApp.log("onKeyDown : " + keyCode);
			return super.onKeyDown(keyCode, event);
		}
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_LEFT:
			MyApp.log("onKeyUp : DPAD_LEFT");
			return true;
		case KeyEvent.KEYCODE_DPAD_UP:
			MyApp.log("onKeyUp : DPAD_UP");
			return true;
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			MyApp.log("onKeyUp : DPAD_RIGHT");
			return true;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			MyApp.log("onKeyUp : DPAD_DOWN");
			return true;
		default:
			MyApp.log("onKeyDown : " + keyCode);
			return super.onKeyUp(keyCode, event);
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return mMyView.onTouchEvent(event);
		/*
		int pointerCount = event.getPointerCount();
		for (int i=0; i<pointerCount; i++) {
			int x = (int)event.getX(i);
			int y = (int)event.getY(i);    		
			int id = event.getPointerId(i);
			int action = event.getActionMasked();
			int actionIndex = event.getActionIndex();
			String actionString = "";

			switch (action) {
			case MotionEvent.ACTION_DOWN:
				actionString = "DOWN";
				break;
			case MotionEvent.ACTION_UP:
				actionString = "UP";
				break;	
			case MotionEvent.ACTION_POINTER_DOWN:
				actionString = "PNTR DOWN";
				break;
			case MotionEvent.ACTION_POINTER_UP:
				actionString = "PNTR UP";
				break;
			case MotionEvent.ACTION_MOVE:
				actionString = "MOVE";
				break;
			default:
				break;
			}

			String touchStatus = "1Action: " + actionString + " Index: " + actionIndex + " ID: " + id + " X: " + x + " Y: " + y;
			MyApp.log(touchStatus);
		}

		return true;
		//return super.onTouchEvent(event);
		 */
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}


	protected class MyView extends View {
		private ArrayList<Vertex> mVertex0 = new ArrayList<Vertex>();
		private ArrayList<Vertex> mVertex1 = new ArrayList<Vertex>();
		private Paint mPaint0 = new Paint();
		private Paint mPaint1 = new Paint();

		public MyView(Context context) {
			super(context);
			setFocusable(true);

			mPaint0.setColor(Color.RED);
			mPaint0.setStrokeWidth(3);
			mPaint0.setAntiAlias(true);

			mPaint1.setColor(Color.BLUE);
			mPaint1.setStrokeWidth(3);
			mPaint1.setAntiAlias(true);
		}

		public boolean onTouchEvent(MotionEvent event) {
			boolean isPaint = false;
			int pointerCount = event.getPointerCount();
			for (int i=0; i<pointerCount; i++) {
				int x = (int)event.getX(i);
				int y = (int)event.getY(i);    		
				int id = event.getPointerId(i);
				int action = event.getActionMasked();

				switch (action) {
				case MotionEvent.ACTION_DOWN:
					mVertex0.add(new Vertex(x, y, false));
					MyApp.log("DOWN : " + id + ", " + mVertex0.size() + ", " + x + ", " + y);
					break;
				case MotionEvent.ACTION_POINTER_DOWN:
					if (0 == id) {
						mVertex0.add(new Vertex(x, y, false));
						MyApp.log("POINTER_DOWN : " + id + ", " + mVertex0.size() + ", " + x + ", " + y);
					} else {
						mVertex1.add(new Vertex(x, y, false));
						MyApp.log("POINTER_DOWN : " + id + ", " + mVertex1.size() + ", " + x + ", " + y);
					}
					break;
				case MotionEvent.ACTION_POINTER_UP:
					break;
				case MotionEvent.ACTION_MOVE:
					if (0 == id) {
						mVertex0.add(new Vertex(x, y, true));
						MyApp.log("MOVE : " + id + ", " + mVertex0.size() + ", " + x + ", " + y);
					} else {
						mVertex1.add(new Vertex(x, y, true));
						MyApp.log("MOVE : " + id + ", " + mVertex1.size() + ", " + x + ", " + y);
					}
					isPaint = true;
					break;
				default:
					break;
				}
			}
			if (isPaint) {
				invalidate();
			}

			return true;
			//return super.onTouchEvent(event);
		}

		public void onDraw(Canvas canvas) {
			canvas.drawColor(Color.LTGRAY);
			for (int i=0; i<mVertex0.size(); i++) {
				if (mVertex0.get(i).draw) {
					canvas.drawLine(mVertex0.get(i-1).x, mVertex0.get(i-1).y,
							mVertex0.get(i).x, mVertex0.get(i).y, mPaint0);
				}
			}
			for (int i=0; i<mVertex1.size(); i++) {
				if (mVertex1.get(i).draw) {
					canvas.drawLine(mVertex1.get(i-1).x, mVertex1.get(i-1).y,
							mVertex1.get(i).x, mVertex1.get(i).y, mPaint1);
				}
			}
		}
	}

	public class Vertex {
		public float x;
		public float y;
		public boolean draw;

		Vertex(float ax, float ay, boolean ad) {
			x = ax;
			y = ay;
			draw = ad;
		}
	}
}
